package com.mediacloud.app.newsmodule.view.share

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.mediacloud.app.assembly.util.QRCodeUtil
import com.mediacloud.app.assembly.util.buildUrl
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.assembly.util.loadXWithGlideCircleCrop
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.utils.share.ShareMemberBeen
import com.mediacloud.app.newsmodule.view.adapter.CardAdapter

/**
 * 媒体号得海报分享
 */
class PosterSharePop3(context: Context) : AbsPosterSharePop(context) {

    val posterRootView: View
    val allView: View
    val logoIv: ImageView
    val titleTv: TextView
    val summaryTv: TextView
//    val article1: TextView
//    val article2: TextView
//    val article3: TextView
    val qrCodeIv: ImageView
    val hotEmpty: TextView
    val hotRecycler: RecyclerView
    val hotLayout: LinearLayout
    val container: FrameLayout
    var cardAdapter: CardAdapter? = null

    init {
        val lp = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)
        lp.addRule(RelativeLayout.CENTER_IN_PARENT)
        posterRootView = LayoutInflater.from(context).inflate(R.layout.share_poster3, null, false)
        logoIv = posterRootView.findViewById(R.id.logoIv)
        titleTv = posterRootView.findViewById(R.id.titleTv)
        container = posterRootView.findViewById(R.id.container)
//        article1 = posterRootView.findViewById(R.id.article1)
//        article2 = posterRootView.findViewById(R.id.article2)
//        article3 = posterRootView.findViewById(R.id.article3)
        summaryTv = posterRootView.findViewById(R.id.summaryTv)
        qrCodeIv = posterRootView.findViewById(R.id.qrCodeIv)
        hotEmpty = posterRootView.findViewById(R.id.hotEmpty)
        hotLayout = posterRootView.findViewById(R.id.hotLayout)
        hotRecycler = posterRootView.findViewById(R.id.hotRecycler)
        allView = posterRootView.findViewById(R.id.allView)
        hotRecycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        cardAdapter = CardAdapter()
        hotRecycler.adapter = cardAdapter
        posterContainer.addView(posterRootView, lp)
        container.setOnClickListener {
            dismiss()
        }
    }

    override fun posterView(): View {
        return allView
    }

    @SuppressLint("SetTextI18n")
    override fun updateData(article: ArticleItem,bitmap: Bitmap?) {

    }

    var arr: ArrayList<String> = arrayListOf()

    fun setAritcleData(arr: ArrayList<String>) {
        this.arr.clear()
        this.arr.addAll(arr)
        if(arr.size>0){
            cardAdapter!!.setNewData(arr)
            hotLayout.visibility = View.VISIBLE
            hotEmpty.visibility = View.GONE
        }else{
            hotLayout.visibility = View.GONE
            hotEmpty.visibility = View.VISIBLE
        }
    }

    fun setData(been: ShareMemberBeen?) {
        if (been == null) return
        posterContainer.visibility = View.VISIBLE
      // logoIv.loadX(been.logo, 1, 0, false)
       logoIv.loadXWithGlideCircleCrop(been.logo, 1,  ContextCompat.getDrawable(context, R.drawable.cctv7_loading_img))

      // Glide.with(context).load(buildUrl(1,been.logo)).into(logoIv)

        titleTv.text = been.name
        summaryTv.text = been.summary
        val qrCode = QRCodeUtil.createQRImage(been.url, qrCodeIv.width, qrCodeIv.height, null)
        qrCodeIv.setImageBitmap(qrCode)

//        if (been.article.isNullOrEmpty()) {
//            hotLayout.visibility = View.GONE
//            return
//        } else {
//            hotLayout.visibility = View.VISIBLE
//        }
//        been.article.forEachIndexed { index, s ->
//            when (index) {
//                0 -> {
//                    article1.visibility = View.VISIBLE
//                    article1.text = s
//                }
//                1 -> {
//                    article2.visibility = View.VISIBLE
//                    article2.text = s
//                }
//                2 -> {
//                    article3.visibility = View.VISIBLE
//                    article3.text = s
//                }
//            }

//        }
    }


}